package com.eascs.spider.ant.common.util;

import java.io.BufferedReader;
import java.io.File;

public class StringUtil {
	static final String DELIM_STR = "{}";
	
	/**
	 * 空格字符串包括中文全角空格跟普通空格
	 * @param msg
	 * @return
	 */
	public static boolean isNullOrEmpty(String msg){
		if(msg == null || msg.equals("") || msg.equals(" ") || msg.equals("　"))
			return true;
		return false;
	}

	public String string2Unicode(String string) {
	    StringBuffer unicode = new StringBuffer();
	    for (int i = 0; i < string.length(); i++) {
	        // 取出每一个字符
	        char c = string.charAt(i);
	        // 转换为unicode
	        unicode.append("\\u" + Integer.toHexString(c));
	    }
	    return unicode.toString();
	}

	public String unicode2String(String unicode) {
	    StringBuffer string = new StringBuffer();
	    String[] hex = unicode.split("\\\\u");
	    for (int i = 1; i < hex.length; i++) {
	        // 转换出每一个代码点
	        int data = Integer.parseInt(hex[i], 16);
	        // 追加成string
	        string.append((char) data);
	    }
	    return string.toString();
	}
	
	public static String getText(String path){
		StringBuffer sb = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new java.io.FileReader(new File(path)));
			String temp = null;
			while((temp = br.readLine()) !=null){
				sb.append(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public static String format(final String messagePattern, Object... arguments) {
		StringBuffer result = new StringBuffer();
		int start = 0;
		int pos = 0;
		for (int i = 0; i < arguments.length; i++) {
			Object object = arguments[i];
			pos = messagePattern.indexOf(DELIM_STR, start);
			result.append(messagePattern.substring(start, pos)).append(object.toString());
			start = pos + 2;
		}
		result.append(messagePattern.substring(start));
		return result.toString();
	}	
	
	public static String stringToAscii(String value) {  
	    StringBuffer sbu = new StringBuffer();  
	    char[] chars = value.toCharArray();   
	    for (int i = 0; i < chars.length; i++) {  
	        if(i != chars.length - 1)  {  
	            sbu.append((int)chars[i]).append(",");  
	        }else {  
	            sbu.append((int)chars[i]);  
	        }  
	    }  
	    return sbu.toString();  
	}
	
	public static void main(String[] args){
		System.out.println(stringToAscii("　"));
	}
}
